import request from '../../utils/request'
// pages/video/video.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navList:[],//用来存储导航菜单的列表数据
    navId:[],//导航菜单的id标识
    videoList:[],//获取播放视频列表
    videoId:'',//存储vid,做查看模式和编辑模式
    updateTimeList:[],//存储对象的,每个对象当中有vid和currentTime
    isTriggered:false//下拉刷新是否开启的标识
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:function (options) {
    // 获取cookies的数据,从缓存中读取出来
    const cookies = wx.getStorageSync('cookies');
    if (!cookies) {
      // 提示用户
      wx.showToast({
        title:'请您登陆',
        icon:'none'
      })
      // 跳转
      wx.navigateTo({
        url:'/pages/login/login'
      })
      return
    }
    // 获取导航菜单
    this.getNavList()
    // console.log(this.data.groupList);
  },
  // 获取列表数据
  async getNavList(){
    const groupListData = await request('/video/group/list')
    const navList = groupListData.data.slice(0,15)
    const navId = navList[0].id
    this.setData({
      navList,
      navId
    })
    // 获取视频标签下对应的视频列表
    this.getVideoList(this.data.navId)
  },
// 点击修改navId的值
  changeNavId(event){
    const navId = event.currentTarget.id
    this.setData({
      navId:navId >>> 0,
      videoList:[]
    })
    this.getVideoList(this.data.navId)
  },
  // 获取视频标签下的对应列表
  async getVideoList(navId){
    // 发送请求时的提示信息
    wx.showLoading({title:'加载中'})
    const videoListData = await request('/video/group',{id:navId})
    // console.log(videoListData);
    // 隐藏提示信息
    wx.hideLoading()
    // 设置下拉刷新关闭
    this.setData({
      isTriggered:false
    })
    let index = 0
    const videoList = videoListData.datas.map(item => {
      item.id = index++
      return item
    })
    this.setData({
      videoList:videoList
    })
    // console.log(this.data.videoList);
  },

  // 点击播放视频时间  bindplay
  handlerPlay(event){
    // 获取每个播放视频的标识(标签),先存储起来
    const vid = event.currentTarget.id
    // 判断当前播放的视频和之前播放的视频是不是同一个
    // this.vid !== vid&&this.videoContext&&this.videoContext.stop()
    // // 将vid存储到this实例身上
    // this.vid = vid
    // 把vid存储贷一个属性中
    this.setData({
      videoId:vid
    })
    // 创建一个播放视频的实例对象
    this.videoContext = wx.createVideoContext(vid)
    // 从数组容器中找到之前存储的对象(vid和currentItem)
    const {updateTimeList} = this.data
    // 看看和存在数组中的id是否有相同id的
    const updateTime = updateTimeList.find(item => item.vid === vid)
    // 直接跳转到之前的播放时间
    this.videoContext.seek(updateTime?updateTime.currentTime:0)
    // 点击自动播放
    this.videoContext.play()
  },
  // 获取实时播放的进度的回调
  handlerUpdate(event){
    const {updateTimeList} = this.data
    // console.log(event);
    // 获取当前正在播放的视频的实时播放的时间
    // const currentTime = event.detail.currentTime
    // 获取视频的标识
    // const vid = event.currentTarget.id
    // 存储到一个对象中
    const updateTime = {
      currentTime:event.detail.currentTime,
      vid:event.currentTarget.id
    }
    // console.log(updateTime.currentTime);
    // 判断当前的容器张宏是否曾经存储过上面这个对象
    const updateTimeItem = updateTimeList.find(item => item.vid === updateTime.vid)
    // 判断这个数组容器中是否存储过这个视频对象(vid和current)
    if (updateTimeItem) {
      updateTimeItem.currentTime = event.detail.currentTime
    }else{
      updateTimeList.push(updateTime)
    }
    
    // 更新数据
    this.setData({
      updateTimeList
    })
  },
  // 当视频播放完毕后执行的回调
  handlerEnd(event){
    // console.log('***');
    const {updateTimeList} = this.data
    
    updateTimeList.splice(updateTimeList.find(item => item.vid === event.currentTarget.vid),1)
    this.setData({
      updateTimeList
    })
  },
  // 下拉滑块的回调
  handlerRefresh(){
    // console.log('***123');
    this.getVideoList(this.data.navId)
  },
  // 上拉刷新
  handlerTolower(){
    const { videoList } = this.data
    // console.log('***');
    // this.getVideoList(this.data.navId)
    const videoArr = [
      {
        "type": 1,
        "displayed": false,
        "alg": "onlineHotGroup",
        "extAlg": null,
        "data": {
          "alg": "onlineHotGroup",
          "scm": "1.music-video-timeline.video_timeline.video.181017.-295043608",
          "threadId": "R_VI_62_A26E70346A58DC9FA2F613B3A259C2C9",
          "coverUrl": "https://p1.music.126.net/QAcgm9QDKzgQS2IwNu4P7g==/109951165356058128.jpg",
          "height": 720,
          "width": 1280,
          "title": "大古时隔多年再次唱响《迪迦奥特曼》主题曲，满满的回忆！",
          "description": null,
          "commentCount": 263,
          "shareCount": 223,
          "resolutions": [
            {
              "resolution": 240,
              "size": 27897226
            },
            {
              "resolution": 480,
              "size": 35378817
            },
            {
              "resolution": 720,
              "size": 36655345
            }
          ],
          "creator": {
            "defaultAvatar": false,
            "province": 110000,
            "authStatus": 0,
            "followed": false,
            "avatarUrl": "http://p1.music.126.net/kBJV7fuExIZCZlm77Y8pLA==/109951163359784378.jpg",
            "accountStatus": 0,
            "gender": 0,
            "city": 110101,
            "birthday": -2209017600000,
            "userId": 1471157370,
            "userType": 0,
            "nickname": "金曲音乐厅",
            "signature": "",
            "description": "",
            "detailDescription": "",
            "avatarImgId": 109951163359784380,
            "backgroundImgId": 109951162868128400,
            "backgroundUrl": "http://p1.music.126.net/2zSNIqTcpHL2jIvU6hG0EA==/109951162868128395.jpg",
            "authority": 0,
            "mutual": false,
            "expertTags": null,
            "experts": {
              "1": "泛生活视频达人"
            },
            "djStatus": 0,
            "vipType": 0,
            "remarkName": null,
            "avatarImgIdStr": "109951163359784378",
            "backgroundImgIdStr": "109951162868128395"
          },
          "urlInfo": {
            "id": "A26E70346A58DC9FA2F613B3A259C2C9",
            "url": "http://vodkgeyttp9.vod.126.net/vodkgeyttp8/SlbWBd0m_1791263860_shd.mp4?ts=1620891293&rid=8ACB7E18ED6E468F511764065ADE1213&rl=3&rs=RugaEXRJyCkjHIJTTySwbriiOZjIDROT&sign=76f40ab70f54ea3931f7a2308a6b31c2&ext=w18ndPq8vPNoZGXIGF38Q0NXlopHU%2FTaDRIkxnE1iaRg4AikplVtHPluAj0q6x03K0lqu5koa%2B1UL0TRUr2WwK%2F8HtO%2FLeCf2K7ww%2BbUrCQUnPoNLd25GyX8rphSRtpgtFJF5iHQpZ84t8Cy4vGoJOAjCIlm2mMJWbLqHLNj9oRN48WcdZOMUSH3AfDkGWckVE%2BNRV2uFJAM71eIFsmOqWmFoPQbyB%2BSUtugltE6YyrBilxbWgbU4tc%2BDPhAnmQf",
            "size": 36655345,
            "validityTime": 1200,
            "needPay": false,
            "payInfo": null,
            "r": 720
          },
          "videoGroup": [
            {
              "id": 58100,
              "name": "现场",
              "alg": null
            },
            {
              "id": 60101,
              "name": "日语现场",
              "alg": null
            },
            {
              "id": 57108,
              "name": "流行现场",
              "alg": null
            },
            {
              "id": 59108,
              "name": "巡演现场",
              "alg": null
            },
            {
              "id": 1100,
              "name": "音乐现场",
              "alg": null
            },
            {
              "id": 5100,
              "name": "音乐",
              "alg": null
            }
          ],
          "previewUrl": null,
          "previewDurationms": 0,
          "hasRelatedGameAd": false,
          "markTypes": null,
          "relateSong": [],
          "relatedInfo": null,
          "videoUserLiveInfo": null,
          "vid": "A26E70346A58DC9FA2F613B3A259C2C9",
          "durationms": 99263,
          "playTime": 296431,
          "praisedCount": 1645,
          "praised": false,
          "subscribed": false
        }
      }
    ]
    if (videoArr.length > 0) {
      videoList.push(...videoArr)
      // 更新数据
      this.setData({
        videoList
      })
    }
  },
  // 点击打开搜索页
  toSearch(){
    wx.navigateTo({
      url:'/pages/search/search'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('页面的下拉刷新');
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('上拉');
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function ({from}) {
    if (from === 'button') {
      // 点击三个点的方式进行转发
      return{
        title:'来自button按钮的转发',
        path:'/pages/video/video',
        imageUrl:'/static/images/lyml.jpg'
      }
    }else {
      // 点击了menu的方式进行转发
      return {
        title: '来自menu按钮的转发',
        path: '/pages/video/video',
        imageUrl:'/static/images/lyml.jpg'
      }
    }
  }
})